package com.keke.rpc.server.provider;

import com.keke.api.UserServiceFacade;
import com.keke.api.vo.UserDTO;
import com.keke.api.vo.UserQueryReq;
import com.keke.rpc.server.cache.UserCache;
import com.keke.rpc.server.db.entity.UserEntity;
import com.keke.rpc.server.db.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.UUID;

@DubboService(version = "1.0.0",group = "${dubbo.application.name}")
@Slf4j
public class UserServiceImpl implements UserServiceFacade {

    @Autowired
    private UserCache userCache;
    @Autowired
    private UserService userService;
    @Override
    public UserDTO getUserDtoByCode(String code) {
        UserEntity user =  userCache.getUser(code);
        UserDTO userDTO = new UserDTO();
        if(user != null){

            userDTO.setUserName(user.getUserName());
            userDTO.setRealName(user.getRealName());
        }
        return userDTO;
    }

    @Override
    public UserDTO getUserDtoByRealName(String realName) {
        UserEntity user = userService.getUserDtoByRealName(realName);
        if(user == null){
            return  null;
        }
        UserDTO userDTO = new UserDTO();
        userDTO.setUserName(user.getUserName());
        userDTO.setRealName(user.getRealName());
        return userDTO;
    }

    @Override
    public UserDTO queryWithCondition(UserQueryReq userQueryReq) {
        log.info("userReq = {}",userQueryReq);
        return new UserDTO();
    }

    @Override
    public Long createUser(UserDTO userDTO) {
        UserEntity queryExistUser = userService.getUserDtoByRealName(userDTO.getRealName());
        if(queryExistUser != null){
            return queryExistUser.getUserId();
        }
        UserEntity user = new UserEntity();

        user.setUserName(userDTO.getUserName());
        user.setRealName(userDTO.getRealName());
        user.setUserCode(UUID.randomUUID().toString().replace("-",""));
        user.setUserPwd("888888");

        userService.save(user);
        return user.getUserId();
    }
}
